package com.qianfeng.service.impl;

import com.qianfeng.cache.SceneCache;
import com.qianfeng.exception.FindDataException;
import com.qianfeng.mapper.SceneMapper;
import com.qianfeng.pojo.BaseUser;
import com.qianfeng.pojo.Scene;
import com.qianfeng.pojo.SysDevice;
import com.qianfeng.pojo.SysUserInfo;
import com.qianfeng.service.DeviceService;
import com.qianfeng.service.SceneService;
import com.qianfeng.status.ResultCode;
import com.qianfeng.utils.LoginUsersUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/9/8 15:33
 */
@Service
public class SceneServiceImpl implements SceneService {
    private DeviceService deviceService;
    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private SceneCache sceneCache;

    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    private SceneMapper sceneMapper;

    @Autowired
    public void setSceneMapper(SceneMapper sceneMapper) {
        this.sceneMapper = sceneMapper;
    }

    @Override
    public void addScene(Scene scene) {
        //将数据插入到数据库就行, 我们的名字可以重复
        //场景是属于用户的,和用户是一对多的关系,创建者就是当前登录的用户
        BaseUser loginUser = LoginUsersUtils.getLoginUser();
        scene.setCreateBy(loginUser.getUserId());
//        添加数据
        sceneMapper.insertScene(scene);
    }

    @Override
    public Scene findById(Long sceneId) {
        //用户只能查询属于自己的场景,所以当前的id必须是自己的场景的id
        BaseUser loginUser = LoginUsersUtils.getLoginUser();
        List<Scene> sceneList=sceneCache.get(loginUser.getUserId());
        Assert.notNull(sceneList,()->{
            throw new FindDataException("当前用户没有这个场景", ResultCode.DATA_NOT_EXIST);
        });
//       通过stream不能返回一个单个对象
        List<Scene> collect = sceneList.stream().filter(scene -> scene.getSceneId().equals(sceneId)).collect(Collectors.toList());
//       一个id只能查到一个场景
        Assert.isTrue(collect.size()==1,()->{
            throw new FindDataException("当前用户没有这个场景",ResultCode.DATA_NOT_EXIST);
        });
        return collect.get(0);
    }

    @Override
    public List<Scene> getSceneAll() {
        BaseUser loginUser = LoginUsersUtils.getLoginUser();
        return sceneMapper.findAll(loginUser.getUserId());
    }

    @Override
    public void deleteById(Long sceneId) {
        BaseUser loginUser = LoginUsersUtils.getLoginUser();
//      查询一下这个场景是否是该用户的
        Scene scene = findById(sceneId);
        Assert.isTrue(scene.getCreateBy().equals(loginUser.getUserId()),()->{
            throw new FindDataException("当前用户没有此场景",ResultCode.DATA_NOT_EXIST);
        });
        //        删除之前判断是否有关联该场景的设备
        List<SysDevice> device =deviceService.findBySceneId(sceneId);
        List<SysDevice> collect = device.stream()
                .filter(d -> {
                   return d.getBindUserId().equals(loginUser.getUserId());
                }).collect(Collectors.toList());
        collect.forEach( d->{
            d.setSceneId( -1L);
            deviceService.updateDevice(d);});
        sceneMapper.deleteById(sceneId);
    }

    @Override
    public void updateById(Scene scene) {
        BaseUser loginUser = LoginUsersUtils.getLoginUser();
//      查询一下这个场景是否是该用户的
        Scene scene1 = findById(scene.getSceneId());
        Assert.isTrue(scene1.getCreateBy().equals(loginUser.getUserId()),()->{
            throw new FindDataException("当前用户没有此场景",ResultCode.DATA_NOT_EXIST);
        });

        sceneMapper.updateById(scene);
    }


}
